﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using WFNetLib;

namespace 无线打分网站.webAPI
{
    public partial class API_Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string name, password;
            JSON_LoginData jld = new JSON_LoginData();
            jld.Teacher = new JSON_TeacherData();
            try
            {
                name = Request.QueryString["name"];
                password = Request.QueryString["password"];
                //信息|用户id|实验id，实验名，实验室;
                if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(password))//没有这两个变量
                {
                    jld.success = false;
                    jld.message = "缺少帐户信息";
                }
                else
                {
                    if (Membership.ValidateUser(name, password))//帐户正确
                    {
                        if (Roles.IsUserInRole(name, "用户"))
                        {
                            MembershipUser user = Membership.GetUser(name);
                            jld.Teacher.ID = user.ProviderUserKey.ToString();
                            UserExData ue = UserExDataDBOption.GetUserExData(new Guid(user.ProviderUserKey.ToString()));
                            if (ue == null)
                            {
                                jld.success = false;
                                jld.message = "缺少教师节信息";
                            }
                            else
                            {
                                jld.Teacher.Name = ue.Name;
                                string[] exids = ue.ExperimentID.Split(';');
                                jld.Experiments = new JSON_ExperimentData[exids.Length];
                                for (int i = 0; i < exids.Length; i++)
                                {
                                    string id = exids[i];
                                    ExperimentData ed = ExperimentDataDBOption.GetExperimentData(new Guid(id));
                                    if (ed == null)
                                    {
                                        jld.success = false;
                                        jld.message = "实验信息有误";
                                    }
                                    else
                                    {
                                        jld.Experiments[i] = new JSON_ExperimentData();
                                        jld.Experiments[i].ID = ed.ID.ToString();
                                        jld.Experiments[i].Name = ed.Name;
                                        jld.Experiments[i].Room = ed.Room;
                                        jld.Experiments[i].Week1 = ed.Week1;
                                        jld.Experiments[i].Week2 = ed.Week2;
                                        jld.Experiments[i].Week3 = ed.Week3;
                                        jld.Experiments[i].Week4 = ed.Week4;
                                        jld.Experiments[i].Week5 = ed.Week5;
                                        jld.Experiments[i].Week6 = ed.Week6;
                                        jld.Experiments[i].Week7 = ed.Week7;
                                        jld.Experiments[i].Times = JsonSerializeHelper.DeSerialize<JSON_ExperimentTime[]>(ed.Times);
                                        jld.success = true;
                                    }
                                }
                            }
                        }
                        else
                        {
                            jld.success = false;
                            jld.message = "帐户组信息有误";
                        }                        
                    }
                    else
                    {
                        jld.success = false;
                        jld.message = "帐户信息有误";
                    }
                }
            }
            catch (System.Exception ex)
            {
                jld.success = false;
                jld.message = "未知错误"+ex.Message;
            }
            Response.Write(JsonSerializeHelper.Serialize(jld));            
        }
    }
}